<template>
  <div class="weater-home">
    <header class="home-header">
      <nav>
        <a href="" @click="$router.push('/')">
          <div class="logo">
            <img src="../img/太阳.png" alt="logo" />
            <p>新中地天气</p>
          </div>
        </a>
        <div class="weather-info">
          <h4>{{ wuhanup.city }}</h4>
          <span
            >实时天气：{{ wuhanup.weather }} {{ wuhanup.temperature }}℃</span
          >
          <span>{{ wuhanup.winddirection }}风{{ wuhanup.windpower }}级</span>
        </div>
        <div class="weather-icon">
          <img src="../img/提示说明.png" alt="" @click="showTip" />
        </div>
        <div v-if="isTipVisible" class="tip-dialog">
          <div class="tip-content">
            <h3>关于:</h3>
            <span>这个应用可以用来追踪你选择城市的当前天气</span>
            <h3>如何使用:</h3>
            <div>1.点击搜索框输入你希望追踪的城市</div>
            <div>2.在搜索结果中选中一个城市，你将获取当地最新的天气</div>
            <div>3.点击右侧的＋号可以将追踪城市的天气情况保存在首页</div>
            <h3>移除城市:</h3>
            <div>如果你不想在首页关注某个城市,可以通过底部的按钮删除它</div>
            <button
              @click="closeTip"
              style="
                height: 30px;
                width: 80px;
                background-color: rgb(0, 102, 138);
                color: white;
                border: none;
                border-radius: 5px;
                margin-top: 20px;
              "
            >
              关闭
            </button>
          </div>
        </div>
      </nav>
    </header>
    <main class="home-main">
      <TodoHeader />
      <TodoList>
        <TodoItem
          @del="handleDelete(index)"
          :weatherList="weatherList"
        ></TodoItem>
      </TodoList>
      <h2 class="recent-weather-title">近期天气</h2>
      <div class="weather-container">
        <div class="weather-item">
          <div
            class="weather-item-title"
            v-for="(weather, index) in wuhanweather"
            :key="index"
          >
            <span>
              {{
                index === 0
                  ? '今日'
                  : index === 1
                  ? '明天'
                  : `周${getDayLabel(weather)}`
              }}
            </span>
            <span>{{ formatDate(weather) }}</span>
            <span>{{ weather.dayweather }}</span>
            <span>风力 {{ weather.daypower }}级</span>
          </div>
        </div>
        <div class="weathercanvas">
          <v-chart :option="weatherOption" ></v-chart>
        </div>
      </div>
    </main>
  </div>
</template>

<script setup>
import { computed, ref, onMounted } from 'vue'
import { useWeatherStore } from '../stores/weather'
import axios from 'axios'
import { useRoute } from 'vue-router'
import TodoHeader from '../components/TodoHeader.vue'
import TodoList from '../components/TodoList.vue'
import TodoItem from '../components/TodoItem.vue'
const isTipVisible = ref(false)
const weatherStore = useWeatherStore()
// 获取天气数据列表
const weatherList = computed(() => weatherStore.weatherList)
// 解构状态, 使用storeToRefs
const route = useRoute()

const weatherOption = ref({})
const wuhanup = ref({})
const wuhanweather = ref([
  {
    date: '',
    week: '',
    dayweather: '',
    daywind: '',
    daypower: '',
    daytemp_float: '',
    nighttemp_float: '',
  },
])

const formatDate = (data) => {
  return data.date.slice(6, 10) || '未知' // 返回处理后的日期
}

const getDayLabel = (data) => {
  const weekMap = {
    1: '一',
    2: '二',
    3: '三',
    4: '四',
    5: '五',
    6: '六',
    7: '日',
  }
  return weekMap[data.week] || '未知' // 返回对应的星期几或 '未知'
}
const showTip = () => {
  isTipVisible.value = true // 显示提示栏
}
const closeTip = () => {
  isTipVisible.value = false // 隐藏提示栏
}
const handleDelete = (index) => {
  weatherStore.removeWeatherData(index)
}
const fetchWeather = async () => {
  try {
    const wuhan1 = await axios.get(
      'https://restapi.amap.com/v3/weather/weatherInfo',
      {
        params: {
          city: '420100',
          key: 'b3c72769ff1bcbdd3d94dca1bff9651c',
          extensions: 'all',
        },
      }
    )
    const wuhan2 = await axios.get(
      'https://restapi.amap.com/v3/weather/weatherInfo',
      {
        params: {
          city: '420100',
          key: 'b3c72769ff1bcbdd3d94dca1bff9651c',
          extensions: 'base',
        },
      }
    )
    const data2 = wuhan1.data.forecasts[0].casts
    const data1 = wuhan2.data.lives[0]
    wuhanup.value = data1
    wuhanweather.value = data2.map((item) => ({
      date: item.date,
      week: item.week,
      dayweather: item.dayweather,
      daywind: item.daywind,
      daypower: item.daypower,
      daytemp_float: item.daytemp_float,
      nighttemp_float: item.nighttemp_float,
    }))
    const dayTemps = wuhanweather.value.map((item) => item.daytemp_float)
    const nightTemps = wuhanweather.value.map((item) => item.nighttemp_float)
    renderLineChart(weatherOption, dayTemps, nightTemps)
  } catch (error) {
    console.error('获取天气信息失败:', error)
  }
}
const renderLineChart = (chartOption, data1, data2) => {
  chartOption.value = {
    xAxis: {
      type: 'category',
      show: true,
      axisLabel: {
        show: false, // 隐藏标签
      },
    },
    yAxis: {
      type: 'value',
      show: false,
    },

    series: [
      {
        data: data1,
        type: 'line',
        smooth: true,
        label: {
          show: true,
          position: 'top',
        },
      },
      {
        data: data2,
        type: 'line',
        smooth: true,
        label: {
          show: true,
          position: 'top',
        },
      },
    ],
  }
}
onMounted(() => {
  fetchWeather()
  const weatherStore = useWeatherStore()
  weatherStore.loadData('weatherList')
})
</script>

<style lang="scss">
* {
  margin: 0;
  padding: 0;
}
.weater-home {
  min-height: 100vh;
  flex-direction: column;
  display: flex;
  background-color: rgb(0 102 138);
  .home-header {
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
    height: 80px;
    position: sticky;

    top: 0px;
    nav {
      display: flex;
      flex-direction: row;
      color: white;
      height: 80px;
      gap: 1rem;
      align-items: center;
      max-width: 1024px;
      width: 100%;
      margin-right: auto;
      margin-left: auto;
      padding-right: 5rem;
      padding-left: 5rem;
      .tip-dialog {
        position: fixed;
        top: -400px;
        left: 0;
        right: 0;
        bottom: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(0, 0, 0, 0.5); /* 半透明背景 */
        z-index: 1000; /* 确保在其他元素之上 */
        .tip-content {
        background: white;
        padding: 20px;
        color: black;
        border-radius: 5px;
        text-align: left;
        
      }
      }
      
      a {
        color: inherit;
        text-decoration: inherit;
        .logo {
          gap: 0.75rem;
          align-items: center;
          display: flex;
          img {
            width: 24px;
            height: 24px;
          }
          p {
            font-size: 1.5rem;
            line-height: 2rem;
            font-weight: 900;
            font-family: 'Font Awesome 6 Free';
          }
        }
      }
      .weather-info {
        display: flex;
        align-items: center;
        gap: 1rem;
        h4 {
          font-size: 1.125rem;
          line-height: 1.75rem;
          margin: 0;
        }
        span {
          font-size: 0.875rem;
          line-height: 1.25rem;
        }
      }
      .weather-icon {
        gap: 0.75rem;
        justify-content: flex-end;
        flex: 1 1 0%;
        display: flex;
        img {
          width: 24px;
          height: 24px;
          cursor: pointer;
        }
      }
    }
  }
  .home-main {
    max-width: 768px;
    width: 100%;
    margin-right: auto;
    margin-left: auto;
    padding-right: 10rem;
    padding-left: 10rem;
  }
  .recent-weather-title {
    font-size: 1.5rem;
    color: white;
  }
  .weather-container {
    padding-left: 3rem;
    padding-right: 3rem;
    padding-top: 2.5rem;
    background: rgb(0 78 113);
    border-radius: 0.25rem;
    margin-top: 0.5rem;
    color: white;
    .weather-item {
      display: flex;
      gap: 1.5rem;
      .weather-item-title {
        text-align: center;
        gap: 1rem;
        flex-direction: column;
        flex: 1 1 0%;
        display: flex;
      }
    }
    .weathercanvas {
      height: 20rem;
      width: 100%;
      margin-top: 2.5rem;
    }
  }
}
</style>
